var app = getApp()
var ConstantData = require('../../../../common/constant_data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // firstShow:true
    // scanSearch:ConstantData.CONSTATNT_DATA.BASE_URL + '/mini/scanSearch.png',
    scanSearch: ConstantData.CONSTATNT_DATA.BASE_URL + '/mini/sanglongjia/2023/scanSearch.png',
    scan: ConstantData.CONSTATNT_DATA.BASE_URL + '/mini/sanglongjia/2023/scan.png',
    hospitalName:null,
    pileCounts: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserNoBackInfo()
    if (options.qrc || options.equId || options.uk){
      this.setData({
        qrc: options.qrc ? decodeURIComponent(options.qrc) :'',
        equId: options.equId ? options.equId:'',
        uk: options.uk ? options.uk : '',
        firstShow: false,
      })
      wx.setNavigationBarTitle({
        title: '位置查询',
      })
      this.getLocations();
    }else {     
      this.setData({
        firstShow: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  scan(){
    let that = this;
    wx.scanCode({
      success(res) {
        var qrc = '';
        var qrCode = res.result;
        let macReg = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;
        if(!qrCode){
          wx.showToast({
            title: '二维码为空',
            icon: 'none'
          })
          return;
        }
        if (qrCode.indexOf(ConstantData.CONSTATNT_DATA.DEVICE_QRCODE_URL_PREFIX) == 0) {
          //设备二维码
          var rst = qrCode.split('/');
          qrc = rst[rst.length - 1];
          that.data.qrc = qrc;
          that.getLocations();
        } else if(macReg.test(qrCode)){
          let mac = qrCode.replace(/:/g, "");
          that.setData({
              mac
          })
          that.getLocations();
        } else {
          wx.showToast({
            title: '二维码错误',
            icon: 'none'
          })
        }
      }
    })
  },
  toLocationDetail: function (e) {
    console.log('toLocationDetail-->', e);
    let locationItem = e.currentTarget.dataset.data;
    locationItem['hosname']= e.currentTarget.dataset.name
    wx.navigateTo({
      url: './locationDetail/locationDetail?locationDetail=' + JSON.stringify(locationItem)
    })
  },
  getUserNoBackInfo() {
    let that = this
    app.executeRequest(function (header) {
        wx.request({
          url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_hospital/userNoBackInfo',
          header: header,
          success(res) {
              console.log('getUserNoBackInfo====>',res)
              let info = res.data.data
              if (info) {
                  that.data.hospitalName = info.hospitalName
                  that.data.equId = info.equId
                  that.getLocations()
              }
          }
        })
    })
  },
  getLocations(){
    let that = this;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_hospital/use_info',
        header: header,
        data: { qrc: that.data.qrc ? that.data.qrc : ''
        , equId: that.data.equId ? that.data.equId : ''
        , mac: that.data.mac ? that.data.mac : ''
        , uk: that.data.uk ? that.data.uk : ''
        },
        success(res) {
          console.log('getLocations--<',res);
          let locationList = res.data;
          if (!locationList) {
            wx.showToast({
              title: '查找失败',
              icon:'none'
            })
            that.setData({
              firstShow: true,
            })
            return;
          }
          that.setData({
            firstShow: false,
          })
          if(locationList.boxCount) {
            //新街口返回的数据信息
            that.setData({
              hospitalName: locationList.hospitalName,
              pileCounts: locationList.boxCount,
              equCountInfoList: locationList.equCountInfoList,
              equUseData: locationList.equUseData
            })

          }else if (locationList.chairList && locationList.chairList.length > 0) {//轮椅
            that.setData({
              chairList: locationList.chairList,
              basicInfo: locationList.basicInfo,
              hospitalName: locationList.basicInfo.hospitalName,
              pileCounts: locationList.basicInfo.pileCounts
            })
            that.data.chairList.forEach(item=>{
              var countsA = item.pileList.filter(item1 => item1.equipCode && item1.equipCode != 0).length;
              item.countsA = countsA;
              item.countsB = item.pileList.length - countsA;
            })
            that.setData({
              chairList: that.data.chairList,
            })
          }
          if (locationList.carList && locationList.carList.length > 0) {//担架
            that.setData({
              carList: locationList.carList,
              basicInfo: locationList.basicInfo,
            })
            that.data.carList.forEach(item => {
              var countsA = item.pileList.filter(item1 => item1.equipCode && item1.equipCode != 0).length;
              item.countsA = countsA;
              item.countsB = item.pileList.length - countsA;
            })
            that.setData({
              carList: that.data.carList,
            })
          }
        },
        fail(res) {
          that.setData({
            firstShow: true,
          })
        }
      })
    })
  }
})